/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PnlPositions.java
 *
 * Created on May 13, 2010, 4:57:33 PM
 */

package org.foment.gem.beans;

import javax.swing.DefaultListModel;
import org.foment.joliage3.newt.KeyValue;

/**
 *
 * @author Dusan
 */
public class PnlPositions extends javax.swing.JPanel {

    /** Creates new form PnlPositions */
    public PnlPositions() {
        initComponents();
        setComponents();
    }

    private void setComponents()
    {
        comboJob.setQuery("SELECT JobID AS keyName, JobTitle AS fieldName FROM Job;");
        comboJob.listenTables("Job");

        comboJobType.setQuery("SELECT JobTypeID AS keyName, JobTypeTitle AS fieldName FROM JobType WHERE "+
              "JobID = " + comboJob.getValue() + ";");
        KeyValue kv = new KeyValue(-100, "Sve");
        comboJobType.insertItemAt(kv, 0);
        comboJobType.listenTables("JobType");
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */

    public String getQuery()
    {
        // ako nista nema u listi, tada ne kreiramo upit
        if (positions.size() == 0)
            return "";
        
        String query = "";
        query += " EmployeeJobType.JobTypeID IN ( ";
        int size = positions.size();
        for (int i = 0; i < size-1; i++)
            query = query + " '" + ((KeyValue)(positions.get(i))).getKey() + "' , ";
        query = query + " '" + ((KeyValue)(positions.lastElement())).getKey() + "' )";

        return query;
    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlPositions = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        listPositions = new org.foment.joliage3.newt.SQLListBox();
        btnAdd = new javax.swing.JButton();
        btnRemove = new javax.swing.JButton();
        comboJob = new org.foment.joliage3.newt.SQLComboBox();
        comboJobType = new org.foment.joliage3.newt.SQLComboBox();

        jScrollPane2.setViewportView(listPositions);

        btnAdd.setText("+");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        btnRemove.setText("-");
        btnRemove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRemoveActionPerformed(evt);
            }
        });

        comboJob.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboJobActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlPositionsLayout = new javax.swing.GroupLayout(pnlPositions);
        pnlPositions.setLayout(pnlPositionsLayout);
        pnlPositionsLayout.setHorizontalGroup(
            pnlPositionsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlPositionsLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlPositionsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(comboJobType, javax.swing.GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE)
                    .addComponent(comboJob, javax.swing.GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 10, Short.MAX_VALUE)
                .addGroup(pnlPositionsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnRemove)
                    .addComponent(btnAdd))
                .addGap(31, 31, 31)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 229, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(41, 41, 41))
        );
        pnlPositionsLayout.setVerticalGroup(
            pnlPositionsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlPositionsLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlPositionsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(pnlPositionsLayout.createSequentialGroup()
                        .addGroup(pnlPositionsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(comboJob, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnAdd))
                        .addGap(18, 18, 18)
                        .addGroup(pnlPositionsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnRemove)
                            .addComponent(comboJobType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(12, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnlPositions, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnlPositions, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        // TODO add your handling code here:
        KeyValue kv = (KeyValue)comboJobType.getSelectedItem();
        if (kv.getKey() == -100)
        {
            for (int i = 0; i < comboJobType.getItemCount(); i++)
                if (comboJobType.getItemAt(i) == kv)
                    continue;
                else
                    positions.addElement(comboJobType.getItemAt(i));
        }
        else
        {
            positions.addElement(comboJobType.getSelectedItem());
            
        }
        listPositions.setModel(positions);
}//GEN-LAST:event_btnAddActionPerformed

    private void btnRemoveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRemoveActionPerformed
        // TODO add your handling code here:   
        positions.removeElement(listPositions.getSelectedValue());
        listPositions.setModel(positions);
}//GEN-LAST:event_btnRemoveActionPerformed

    private void comboJobActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboJobActionPerformed
        // TODO add your handling code here:
        comboJobType.setQuery("SELECT JobTypeID AS keyName, JobTypeTitle AS fieldName FROM JobType WHERE "+
                "JobID = "+comboJob.getValue()+";");
        KeyValue kv = new KeyValue(-100, "Sve");
        comboJobType.insertItemAt(kv, 0);
}//GEN-LAST:event_comboJobActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnRemove;
    private org.foment.joliage3.newt.SQLComboBox comboJob;
    private org.foment.joliage3.newt.SQLComboBox comboJobType;
    private javax.swing.JScrollPane jScrollPane2;
    private org.foment.joliage3.newt.SQLListBox listPositions;
    private javax.swing.JPanel pnlPositions;
    // End of variables declaration//GEN-END:variables

    java.util.Vector<Integer> addedPositions = new java.util.Vector<Integer>();
    java.util.Vector<String> addedCourses = new java.util.Vector<String>();
    DefaultListModel positions = new DefaultListModel();
}
